<?php
/* 定义属性 */
define ("CONfilename", "../config/set.php");
define ("DDUDB","../class/db.class.php");
define ("LOADINDEX","../class/load.class.php");
/* 為系統作配置準備，方便以後系統引用數據庫 */
session_start();
require_once(CONfilename);/*讀設置*/
require_once(DDUDB);/* 載入數據庫模塊 */
require_once(LOADINDEX);/* 載入引導模塊 */

/* 配置屬性 */
$returnajaxcontent ="";
/*下面建立數據連接，添加數據庫模塊到變量，使db變量可以直接控制sql。  */
$db = new CDB();
$MEMBER = new member();
$ROOT = new root();
/* 收到信息 */
if(!empty($_GET)){
	$action = $_GET['action'];
	switch ($action){
		case 'shownoc':
			include './djnoc.html';
			break;
		case 'showgd':
			include './djgd.html';
			break;
		case 'showsmj':
			include './djsmj.html';
			break;
		case 'listnoc':
			listnoc($_POST);
			break;
		case 'listgd':
			listgd($_POST,1);
			break;
		case 'listsmj':
			listgd($_POST,2);
			break;
		case 'viewgd':
			viewgd($_GET['cn'],$_GET['time']);
			break;
		case 'sumbitprogress':
			sumbitgdpg($_GET['cn'],$_GET['time'],$_POST);
			break;
		case 'progress':
			showinprogress($_GET['cn'],$_GET['time']);
			break;
		case 'submitzthhgd':
			submitzthhgd($_GET['cn'],$_GET['time'],$_POST);
			break;
		case 'zthh':
			showzthh($_GET['cn'],$_GET['time'],'gd');
			break;
		case 'zthhnoc':
			showzthh($_GET['cn'],$_GET['time'],'noc');
			break;
		case 'updatehj':
			updatehj($_GET['cn'],$_GET['begintime']);
			break;
		case 'updatezthhgd':
			submitzthhud($_GET['cn'],$_GET['time'],$_POST);
			break;
		case 'resetnocadd':
			$model = true;
			include './npfe_nocadd.html';
			break;
		case 'resetnocedit':
			$model = true;
			nocgdedit($_GET['snid'],$_GET['time']);
			break;
		case 'nocaddsubmit':
			$model = true;
			addnoc($_POST);
			break;
		case 'noceditsubmit':
			$model = true;
			editnoc($_GET['snid'],$_GET['time'],$_POST);
			break;
	}
}
function getdmt(){
	$y=date("Y",time());
	$m=date("m",time());
	$d=date("d",time());
	$t0=date('t');           // 本月一共有几天
	$t1=mktime(0,0,0,$m,1,$y);        // 创建本月开始时间
	$t2=mktime(23,59,59,$m,$t0,$y);       // 创建本月结束时间
	$rdate['today'] = date("Y-m-d",time());
	$rdate['dmtb'] = date("Y-m-d H:i:s",$t1);
	$rdate['dmte'] = date("Y-m-d H:i:s",$t2);
	$rdate['sjc'] = $t2-$t1;
	return $rdate;
}
function nocgdedit($snid,$time){
	global $db;
	$html = file_get_contents('./npfe_nocedit.html');
	$table_change=array(
			'{snid}' => $snid,
			'{time}' => $time,
	);
	$sql = "select * from cu_noc where snid = '$snid' && time = '$time'";
	$db->ExeQuerySql($sql);
	if ($db->FetchRow()) {
		$table_change['{cname}'] = $db->Get('cname');
		$table_change['{cnumber}'] = $db->Get('cnumber');
		$table_change['{fanying}'] = $db->Get('fanying');
		$table_change['{huifu}'] = $db->Get('huidan');
		$table_change['{gdlb}'] = $db->Get('gdlb');
	}else {
		$table_change['{cname}'] = '获取失败';
		$table_change['{cnumber}'] = '获取失败';
		$table_change['{fanying}'] = '获取失败';
	}
	$db->FreeResult();
	$html=strtr($html,$table_change);
	echo $html;
}
function formattime($stt,$ett){
	$formated['stt'] = date('Y-m-d H:i:s',strtotime($stt));
	$formated['ett'] = date('Y-m-d H:i:s',strtotime($ett));
	return $formated;
}
function viewgd($cn,$time){
	global $db;
	$xml = file_get_contents("../tpl/tpl.viewgd.html");
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$table_change=array(
				'{content}' => $db->Get('content'),
				'{orderman}' => $db->Get('orderman'),
				'{isend}' => $db->Get('isend'),
				'{archivecontent}' => $db->Get('archivecontent'),
				'{ctrlman}' => $db->Get('ctrlman'),
				'{customername}' => $db->Get('customername'),
				'{submitnumber}' => $db->Get('submitnumber'),
				'{contactnumber}' => $db->Get('contactnumber'),
				'{idnumber}' => $db->Get('idnumber'),
				'{idcardaddress}' => $db->Get('idcardaddress'),
				'{qctr}' =>getlinkqc($cn),
				'{cn}' => $cn,
				'{time}' =>$time,
				'{hjtr}' =>serachhj($cn),
		);
		$xml=strtr($xml,$table_change);
	}
	echo $xml;
}
function serachhj($cn){
	global $db;
	$df = '<tr><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无权</td></tr>';
	$xml = '';
	$db->ExeQuerySql("SELECT * FROM `cu_pg` WHERE  gd_cn = '$cn'");
	for ($i = 0; $i < $db->db->row_count; $i++) {
		$row = $db->GetFetchArray();
		$xml=$xml.'<tr><td>'.$row['pg_name'].'</td><td>'.$row['pg_begintime'].'</td><td>'.$row['pg_rechtime'].'</td><td>'.$row['pg_yjbx'].'</td><td>'.$row['pg_sjbx'].'</td><td>'.$row['traceman'].'</td><td>'.$row['oman'].'</td><td>'.$row['bz'].'</td><td>'.$row['bh'].'</td><td><a href="javascript:void(0)" onclick="updatehj(\''.$cn.'\',\''.$row['pg_begintime'].'\')">更正</a></td></tr>';
	}
	if($xml==''){
		$xml = $df;
	}
	return $xml;
}
function getlinkqc($cn){
	global $db;
	$xml = '';
	$db->ExeQuerySql("SELECT * FROM `cu_qcxq` WHERE  checknumber='$cn'");
	for ($i = 0; $i < $db->db->row_count; $i++) {
		$row = $db->GetFetchArray();
		$xml=$xml.'<tr><td>'.$row['hwyname'].'</td><td>'.$row['clname'].'</td><td>'.$row['headman'].'</td><td>'.$cn.'</td><td>'.$row['sn'].'</td><td><a href="'.$row['link'].'" target="new">链接</a></td><td>'.$row['baseappraise'].'</td></tr>';
	}
	if($xml==''){
		$xml = '<tr><td>无</td><td>无</td><td>无</td><td>'.$cn.'</td><td>无</td><td>无</td><td>无</td></tr>';
	}
	return $xml;
}
function sumbitgdpg($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$db->ExeSql("update `cu_glxq` set ctrlman='$posti[chuliren]',isend='$posti[gdlb]',endtime=now(),archivecontent='$posti[archivecontent]',gdqc='$posti[gdqc]',xsygs='$posti[xsygs]' where contactnumber='$cn' AND time='$time'");
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的工单已处理，请重新查询！</div>';
	}else{
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
	}
	echo $xml;
}
function showinprogress($cn,$time){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if($gdclroot==true){
		$xml = file_get_contents("./npfe_tpl_sips.html");
		$table_change=array(
			'{cn}' => $cn,
			'{time}' => $time,
			'{now_em}' => $now_em
		);
		$xml=strtr($xml,$table_change);
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div><img src="../../image/style/t2.png" /><br>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	echo $xml;
}
function submitzthhgd($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$c = $posti['radio'] = 'bha'?'true':'false';//判断操作者是否选择“已闭环”
		$db->ExeSql("insert into cu_pg(gd_cn,gd_time,pg_name,pg_rechtime,pg_begintime,pg_yjbx,pg_sjbx,traceman,oman,bz,bh) values ('$cn','$time','$posti[hjlb]','$posti[rechtime]',now(),'$posti[yjbx]','$posti[sjbx]','$posti[traceman]','$posti[oman]','$posti[hjbz]','$c')");
		$db->ExeSql("update cu_glxq set hhj=true where contactnumber='$cn'");
		$db->FreeResult();
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的环节已提交，请重新查询！</div>';
	}else{
		//当找不到对应的工单时，自动转到省版工单查询
		$db->ExeQuerySql("SELECT * FROM `cu_noc` WHERE  cnumber='$cn' AND time='$time'");
		if ($db->FetchRow()) {
			# 二级查询，确认投诉来源
			$c = $posti['radio'] = 'bha'?'true':'false';//判断操作者是否选择“已闭环”
			$db->ExeSql("insert into cu_pg(gd_cn,gd_time,pg_name,pg_rechtime,pg_begintime,pg_yjbx,pg_sjbx,traceman,oman,bz,bh) values ('$cn','$time','$posti[hjlb]','$posti[rechtime]',now(),'$posti[yjbx]','$posti[sjbx]','$posti[traceman]','$posti[oman]','$posti[hjbz]','$c')");
			$db->ExeSql("update cu_noc set hhj = 'true' where cnumber='$cn'");
			$db->FreeResult();
			$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的环节已提交，请重新查询！</div>';
		}else{
			$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
		}
	}
	echo $xml;
}
function submitzthhud($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_pg` WHERE  gd_cn='$cn' AND pg_begintime='$time'");
	if ($db->FetchRow()){
		$c = $posti['radio'] = 'bha'?'true':'false';//判断操作者是否选择“已闭环”
		$db->ExeSql("update cu_pg set pg_rechtime='$posti[rechtime]',pg_yjbx='$posti[yjbx]',pg_sjbx='$posti[sjbx]',traceman='$posti[traceman]',oman='$posti[oman]',bz='$posti[hjbz]',bh='$c' where gd_cn='$cn' and pg_begintime='$time'");
		$db->ExeSql("update cu_glxq set hhj=true where contactnumber='$cn'");
		$db->FreeResult();
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的环节已提交，请重新查询！</div>';
	}else{
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
	}
	echo $xml;
}
function showzthh($cn,$time,$md){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if ($gdclroot==true) {
		switch ($md) {
			case 'gd':
			//显示内部工单的环节增加窗口内容
				$db->ExeQuerySql("select * from cu_glxq where contactnumber='$cn' and time='$time'");
				if ($db->FetchRow()) {
					if ($db->Get('isend')=="归档") {
						$xml = file_get_contents("./npfe_zthh.html");
						$table_change=array(
							'{cn}' => $cn,
							'{time}' => $time,
							'{now_em}' => $now_em,
							'{now_om}' => $db->Get('customername')
						);
						$xml=strtr($xml,$table_change);
					}else{
						$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前的工单没有归档，请确认归档之后再处理退换货！</div>';
					}
				}
				break;
			case 'noc':
				//显示省版工单的环节增加窗口内容
				$db->ExeQuerySql("select * from cu_noc where cnumber='$cn' and time='$time'");
				if ($db->FetchRow()) {
					$xml = file_get_contents("./npfe_zthh.html");
					$table_change=array(
						'{cn}' => $cn,
						'{time}' => $time,
						'{now_em}' => $now_em,
						'{now_om}' => $db->Get('cname')
					);
					$xml=strtr($xml,$table_change);
				}else{
					$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，没有找到需要处理的工单，可能是在处理之前就被删除！</div>';
				}
				break;
			
			default:
				# code...
				break;
		}
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	echo $xml;
}
function updatehj($cn,$time){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if($gdclroot==true){
		$db->ExeQuerySql("select * from cu_pg where gd_cn='$cn' and pg_begintime='$time'");
		if ($db->FetchRow()) {
			$xml = file_get_contents("./npfe_zthh_edithj.html");
			$table_change=array(
					'{cn}' => $cn,
					'{time}' => $time,
					'{now_em}' => $now_em,
					'{now_om}' => $db->Get('oman'),
					'{pg_name}' => $db->Get('pg_name'),
					'{hjbz}' =>$db->Get('bz')
			);
			$xml=strtr($xml,$table_change);
		}
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	echo $xml;
}
function addnoc($posti){
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$sql = "INSERT INTO `cu_noc` (`snid`, `cname`, `cnumber`, `overtime`, `isduban`, `fanying`, `huidan`, `gdlb`, `bz`, `time`, `xsygs`, `ctlman`) VALUES ('$posti[snid]', '$posti[cname]', '$posti[cnumber]', '$posti[overtime]', '$posti[isduban]', '$posti[fanying]', '$posti[huidan]', '$posti[gdlb]', '$posti[bz]', now(), '$posti[xsygs]', '$now_em');";
	try {
		$db->ExeSql($sql);
		$db->ExeQuerySql("select snid from cu_noc where snid='$posti[snid]'");
		if ($db->FetchRow()) {
			echo "系统已成功添加纪录！<a href=\"javascript:void(0)\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" onclick=\"resetnoc_add()\">返回继续新增工单</a>";
		}
	} catch (Exception $e) {
		if ($e->g=="") {
			print $e->getMessage();
		}
	}
	$db->FreeResult();
}
function editnoc($snid,$time,$posti){
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$sql = "UPDATE `cu_noc` SET `snid`='$snid', `cname`='$posti[cname]', `cnumber`='$posti[cnumber]', `overtime`='$posti[overtime]', `isduban`='$posti[isduban]', `fanying`='$posti[fanying]', `huidan`='$posti[huidan]', `ctlman`='$now_em', `gdlb`='$posti[gdlb]', `bz`='$posti[bz]', `time`='$time', `xsygs`='$posti[xsygs]' WHERE (`snid`='$snid ') AND (`time`='$time') LIMIT 1;";
	try {
		$db->ExeSql($sql);
		$db->ExeQuerySql("select snid from cu_noc where snid='$snid'");
		if ($db->FetchRow()) {
			echo "系统已成功修改纪录！<a href=\"javascript:void(0)\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" onclick=\"resetnoc_add()\">返回继续新增工单</a>";
		}
	} catch (Exception $e) {
		if ($e->g=="") {
			print $e->getMessage();
		}
	}
	$db->FreeResult();
}